/**
 * 
 */
package wangjg.xuexiban.activity.exam.layer;

import wangjg.game.android.AndroidCanvas;
import wangjg.game.layer.Graphics;
import wangjg.game.widget.Widget;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
/**
 * @author wangjg
 *
 */
public class LabelBox extends Widget{
	private String text;
	
	public LabelBox(float widthPercent, float heightPercent) {
		super(widthPercent, heightPercent, 0);
	}
	
	public LabelBox(float widthPercent, float heightPercent, int spacing) {
		super(widthPercent, heightPercent, spacing);
	}

	public void paint(Graphics g) {
		g.setColor(0xffCCCC00);
		g.drawRect(pp.getAbsX(), pp.getAbsY(), pp.getAbsWidth(), pp.getAbsHeight());
		g.setColor(0xff00ff00);
		this.drawText(g);
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	private TextUtil getTextUtil(){
		TextUtil mTextUtil = new TextUtil(text, pp.getAbsX(), pp.getAbsY(), pp.getAbsWidth(), pp.getAbsHeight(), 0xFFffffff, 0xFF000000, 255, 16);
        mTextUtil.InitText();
        return mTextUtil;
	}
	
	private void drawText(Graphics g) {
		Canvas canvas = ((AndroidCanvas)g).getCanvas();
		drawText3(canvas);
		
//		TextUtil tu = this.getTextUtil();
//		tu.DrawText(canvas);
	}

	private void drawText2(Graphics g) {
		int width = 10;
		int height = 10;
		Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint();
		canvas.drawText(text, 0, 0, paint);

		canvas.save(Canvas.ALL_SAVE_FLAG);
		canvas.restore();
	}
	
	public void drawText3(Canvas canvas) {  
	    Rect targetRect = new Rect(pp.getAbsX(), pp.getAbsY(), pp.getAbsX()+pp.getAbsWidth(), pp.getAbsY()+pp.getAbsHeight());  
	    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
	    paint.setStrokeWidth(3);  
	    paint.setTextSize(16);  
	    String testString = "测试：ijkJQKA:1234";  
	    paint.setColor(Color.CYAN);  
	    canvas.drawRect(targetRect, paint);  
	    paint.setColor(Color.RED);  
	    FontMetricsInt fontMetrics = paint.getFontMetricsInt();  
	        // 转载请注明出处：http://blog.csdn.net/hursing  
	    int baseline = targetRect.top + (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;  
	    // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()  
	    paint.setTextAlign(Paint.Align.CENTER);  
	    canvas.drawText(testString, targetRect.centerX(), baseline, paint);  
	}  
}